package com.right.health.app.widget;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.right.health.app.R;

public class ProgressLayout extends RelativeLayout {
    public static final int STATUS_INFO = 1;
    public static final int STATUS_LOADING = 2;
    private LinearLayout loadingView;
    private LinearLayout infoView;
    private LayoutInflater mInflater;
    private LayoutParams layoutParams;
    private int mStatus;
    private TextView mRefresh, mInfoTv;
    private ImageView mImageView;

    public void setImgId(int imgId) {
        mImgId = imgId;
        if (mImageView != null&&mImgId!=0) {
            try {
                mImageView.setImageResource(mImgId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private int mImgId;

    public ProgressLayout(Context context) {
        super(context);
        mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public ProgressLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public ProgressLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void showLoading() {
        if (infoView != null && infoView.getVisibility() == VISIBLE) {
            infoView.setVisibility(GONE);
        }
        if (loadingView == null) {
            initLoadingView();
        } else {
            loadingView.setVisibility(VISIBLE);
        }
        mStatus = STATUS_LOADING;
    }

    public void showInfo() {
        this.setVisibility(VISIBLE);
        if (loadingView != null && loadingView.getVisibility() == VISIBLE) {
            loadingView.setVisibility(GONE);
        }
        if (infoView == null) {
            initInfoView();
        } else {
            infoView.setVisibility(VISIBLE);
        }
        mStatus = STATUS_INFO;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }


    public int getStatus() {
        return mStatus;
    }

    public void setRefreshClick(OnClickListener listener) {
        if (mRefresh == null) {
            initInfoView();
        }
        mRefresh.setOnClickListener(listener);
        mRefresh.setVisibility(VISIBLE);
        postInvalidate();
    }

    private void initLoadingView() {
        loadingView = (LinearLayout) mInflater.inflate(R.layout.layout_loading_view, null);
        layoutParams = new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        loadingView.setLayoutParams(layoutParams);
        this.addView(loadingView, layoutParams);
    }

    private void initInfoView() {
        infoView = (LinearLayout) mInflater.inflate(R.layout.layout_info_view, null);
        mRefresh =  infoView.findViewById(R.id.tv_refresh);
        mImageView = infoView.findViewById(R.id.imageView);
        mInfoTv = infoView.findViewById(R.id.tv_info);
        try {
            if (mImgId != 0) {
                mImageView.setImageResource(mImgId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        layoutParams = new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        infoView.setLayoutParams(layoutParams);
        this.addView(infoView);
    }

    /**
     * 展示内容
     *
     * @param content
     */
    public void showContent(View... content) {
        this.setVisibility(View.GONE);
        if (content != null) {
            for (int i = 0; i < content.length; i++) {
                content[i].setVisibility(View.VISIBLE);
            }

        }
    }


    /**
     * 显示错误引导页
     *
     * @param content
     */
    public void showInfo(View content, String info) {
        this.showInfo(info);
        content.setVisibility(View.GONE);
    }

    public void showInfo(String info) {
        showInfo();
        mInfoTv.setText(info);
    }


    public void showInfo(View content) {
        this.showInfo();
        content.setVisibility(View.GONE);
    }

    public void hide() {
        this.setVisibility(GONE);
        if (mInfoTv != null && !TextUtils.isEmpty(mInfoTv.getText())) {
            mInfoTv.setText("");
        }
    }

    /**
     * 显示加载页面
     *
     * @param content
     */
    public void showLoading(View content) {
        this.setVisibility(View.VISIBLE);
        this.showLoading();
        content.setVisibility(View.GONE);
    }

}
